package monitor.view.bean;

import javax.faces.bean.ManagedBean;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * JSF ManagedBean used in User Authentication
 *
 */
@ManagedBean(name="authentication")
public class AuthenticationController extends Base {
	
	private static final long serialVersionUID = 5036703856906516317L;
	
	private UserService userService = UserServiceFactory.getUserService();
	private User user = userService.getCurrentUser();
	
	public boolean isAuthenticated() {
		if(this.user != null) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * @return logout URL
	 */
	public String getLogoutURL() {
		String requestURI = getRequest().getRequestURI();
		return this.userService.createLogoutURL(requestURI);
	}
	
	/**
	 * @return URL to login form
	 */
	public String getLoginURL() {
		String requestURI = getRequest().getRequestURI();
		return this.userService.createLoginURL(requestURI);
	}
	
	/**
	 * @return nickname of logged in user
	 */
	public String getNickname() {
		if(this.user != null) {
			return this.user.getNickname();
		} else {
			return "";
		}
	}
}
